/*
 * Copyright (c) zero. 2022-2023. All rights reserved.
 *
 */

package com.zero.tool.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ThreadPoolUtils {

    private static Logger log = LoggerFactory.getLogger(ThreadPoolUtils.class);

    public static int getIndexOfCurrentThread(ThreadPool pool) {
        List<Long> workers = pool.getWorkers();
        for (int i = 0; i < workers.size(); i++) {
            if (workers.get(i).equals(Thread.currentThread().getId())) {
                return i;
            }
        }
        log.error("this thread no found in data array");
        throw new RuntimeException("this thread no found in data array");
    }

    public static int getWaitingStateThreadCount(ThreadPool threadPool) {

        int cnt = 0;
        for (ThreadState workerState : threadPool.getWorkersSign()) {
            if (workerState.equals(ThreadState.WAIT)) {
                ++cnt;
            }
        }
        return cnt;
    }
}
